package me.idashu.attg.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.idashu.attg.domain.LabelEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.domain.UpLabelEntity;
import me.idashu.attg.service.LabelSerivce;
import me.idashu.attg.service.UpLabelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/label")
public class LabelController {
	@Autowired
    private UpLabelService uplabelService;
	
    @Autowired
    private LabelSerivce labelSerivce;
    @RequestMapping("/addLabel")
    public String preAddLabel(Model model,HttpServletRequest request){
    	try{
    		List<UpLabelEntity> listUplabel = uplabelService.getAllUplabe();
    		model.addAttribute("listUplabel", listUplabel);
    		model.addAttribute("addlabel", "添加小分类");
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "add_label";
    }
    
    @RequestMapping("/addLabelInfo")
    public String preAddLabelInfo(@ModelAttribute LabelEntity labelEntity,Model model,HttpServletRequest request){
    	try{
    		labelEntity.setLabel_date(new Date());
    		labelEntity.setLabel_seq(0);
    		labelEntity.setLabel_status("E");
    		labelSerivce.insertLabelEntity(labelEntity);
    		model.addAttribute("success", "添加小分类成功");
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "success";
    }
    @RequestMapping("/getLabelList")
    public String getLabelList(@RequestParam("uplabelId") int uplabelId,PageEntity pageEntity,Model model,HttpServletRequest request){
    	try{
    		int total = labelSerivce.getCountLabelList(uplabelId);
    		pageEntity.setTotalPage(total);
    		pageEntity.setPageLess(pageEntity.getPage());
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("pageEntity", pageEntity);
    		map.put("uplabelId", uplabelId);
    		List<LabelEntity> listlabel = labelSerivce.getLabelList(map);
    		if(listlabel.size()>0){
    			model.addAttribute("listlabel", listlabel);
    		}else{
    			model.addAttribute("listlabel", null);
    		}
    		model.addAttribute("uplabelId", uplabelId);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "labelAjaxList";
    }
    
    @RequestMapping("/getLabelList1")
    public String getLabelList1(@RequestParam("uplabelId") int uplabelId,PageEntity pageEntity,Model model,HttpServletRequest request){
    	try{
    		int total = labelSerivce.getCountLabelList(uplabelId);
    		pageEntity.setTotalPage(total);
    		pageEntity.setPageLess(pageEntity.getPage());
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("pageEntity", pageEntity);
    		map.put("uplabelId", uplabelId);
    		List<LabelEntity> listlabel = labelSerivce.getLabelList(map);
    		if(listlabel.size()>0){
    			model.addAttribute("listlabel", listlabel);
    		}else{
    			model.addAttribute("listlabel", null);
    		}
    		model.addAttribute("uplabelId", uplabelId);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "labelAjaxList1";
    }
}
